package com.zlcx.tz.live.web;

import com.google.common.collect.Lists;
import com.zlcx.tz.live.common.annotation.AuthSig;
import com.zlcx.tz.live.client.UserClient;
import com.zlcx.tz.live.common.annotation.AutoLog;
import com.zlcx.tz.live.exception.CheckParamException;
import com.zlcx.tz.live.exception.CheckRunException;
import com.zlcx.tz.live.vo.ResultVO;
import io.swagger.annotations.*;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>TODO</p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author zl
 * @version 1.0
 * @date Created in 2019/4/15 15:52
 * @copyright: Copyright (c) founders
 */
@Controller
@Slf4j
@Api(tags = {"test"}, description = "test desc")
public class TestController extends BaseController {


    @Autowired
    HttpServletRequest request;

    @Autowired
    HttpServletResponse response;

    @Autowired
    RedisTemplate redisTemplate;


    @Autowired
    UserClient userClient;

    @AutoLog
    @GetMapping("test")
    public ResponseEntity<ResultVO> test(){
        try {
            Thread.sleep(60 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }


    @PreAuthorize("isAuthenticated()")
    @GetMapping("test2")
    public ResponseEntity<ResultVO> test2(){

        return ResponseEntity.ok(successState("success!"));
    }

    @AuthSig
    @ApiOperation(value = "test api", notes = "test api desc",response=TestResultVO.class,responseContainer = "List")
    @GetMapping(value = "/testapi")
    public ResponseEntity<ResultVO> testapi(@ApiParam(value = "签名串", required = true) @RequestHeader String sig,
                                            @ApiParam(value = "id", required = true) @RequestParam String id,
                                            @ApiParam(value = "名称", required = true) @RequestParam String name ){


        if(name.length()>100){
            throw new CheckParamException("名称不能超过100！");
        }


        try {

            //dosomething

            return ResponseEntity.ok(successState(Lists.newArrayList(new TestResultVO(),new TestResultVO())));

        } catch (CheckParamException e) {
            throw e;
        } catch (Exception e) {
            log.error("test api failed!", e);
            throw new CheckRunException("test api failed!", e);
        }

    }

    @AuthSig
    @ApiOperation(value = "test api post", notes = "test api desc",response=TestResultVO.class,responseContainer = "Map")
    @PostMapping(value = "/testapi")
    public ResponseEntity<ResultVO> testapi(@ApiParam(value = "签名串", required = true) @RequestHeader String sig,
                                            @ RequestBody TestParamVO testParamVO){


        if(testParamVO.getName().length()>100){
            throw new CheckParamException("名称不能超过100！");
        }


        try {

            //dosomething

            return ResponseEntity.ok(successState(new TestResultVO()));

        } catch (CheckParamException e) {
            throw e;
        } catch (Exception e) {
            log.error("test api failed!", e);
            throw new CheckRunException("test api failed!", e);
        }

    }


    @Data
    @ToString
    @ApiModel(value="参数")
    class TestParamVO{

        @ApiModelProperty(value="id")
        private Long id;

        @ApiModelProperty(value="名称")
        private String name;

    }

    @Data
    @ToString
    @ApiModel(value="响应")
    class TestResultVO{
        @ApiModelProperty(value="id")
        private Long id;

        @ApiModelProperty(value="名称")
        private String name;

    }



}
